package com.bw.lyc.mvp.view.mine;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.LogUtils;
import com.bw.lyc.R;
import com.bw.lyc.mvp.model.db.DBManager;
import com.bw.lyc.mvp.model.db.UserEntityDao;
import com.bw.lyc.mvp.model.entity.UserEntity;
import com.bw.lyc.util.StatusBarColorUtils;
import com.bw.lyc.util.ToastUtils;

import java.util.List;

public class SafeActivity extends StatusBarColorUtils implements View.OnClickListener {

    private ImageView viewTopBack;
    private Button mineSettingExit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_safe);
        initView();
        viewTopBack.setOnClickListener(this::onClick);
        mineSettingExit.setOnClickListener(this::onClick);
        try {
            LogUtils.json(DBManager.getInstance().getDaoSession().loadAll(UserEntity.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
        setStatusBarColor(Color.WHITE);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.mine_setting_exit) {
            try {
                List<UserEntity> list = DBManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).where(UserEntityDao.Properties.Id.eq(0)).list();
                if (list.size() == 0) {
                    ToastUtils.showMsg("记性真差，您还未登录，请先登录");
                } else {
                    DBManager.getInstance().getDaoSession().deleteAll(UserEntity.class);
                    startPage(LoginActivity.class);
                    finish();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (id == R.id.view_top_back) {
            startPage(SettingActivity.class);
            finish();
        }
    }

    private void initView() {
        viewTopBack = (ImageView) findViewById(R.id.view_top_back);
        mineSettingExit = (Button) findViewById(R.id.mine_setting_exit);
    }

    @Override
    public View addStatusView() {
        return f(R.id.safe_relative);
    }
}